package com.message.api.group.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.annotation.SysLog;
import com.base.helper.BaseController;
import com.base.helper.Result;
import com.message.api.group.entity.AcModule;
import com.message.api.group.service.AcModuleService;
import com.message.api.group.vo.AcModuleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mzl
 * @since 2020-07-09
 */
@Api(tags = "模块")
@RestController
@RequestMapping("/acModule")
public class AcModuleController extends BaseController {

    private static final Logger          LOG = LoggerFactory.getLogger(AcModuleController.class);
    @Autowired
    private              AcModuleService entityService;

    @SysLog(action = "acModule:findByPage", value = "角色管理：模块列表")
    @ApiOperation(value = "分页查询模块", notes = "分页查询模块")
    @RequestMapping(method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "size", value = "分页大小", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面：从1开始", paramType = "query")})
    public Result<IPage<AcModuleVo>> findByPage(final AcModuleVo example, final Page page) {
        IPage<AcModuleVo> records = entityService.findByPage(example, page);
        return new Result(true, records);
    }

    @SysLog(action = "acModule:delete", value = "角色管理：删除模块")
    @ApiOperation(value = "删除模块")
    @RequestMapping(value = "{id}", method = {RequestMethod.DELETE})
    @ApiImplicitParam(name = "id", value = "模块ID", required = true, paramType = "path")
    public Result delete(@PathVariable final Integer id) {
        AcModule acModule = entityService.getById(id);
        if (null == acModule) {
            throw new RuntimeException("没有找到id对应对象");
        }
        acModule.setState("0");
        entityService.updateById(acModule);
        return new Result(true, "成功删除模块", null);
    }

    @SysLog(action = "acModule:one", value = "角色管理：查询模块详情")
    @ApiOperation(value = "查询单个模块")
    @RequestMapping(value = "{id}", method = {RequestMethod.GET})
    @ApiImplicitParam(name = "id", value = "模块ID", required = true, paramType = "path")
    public Result one(@PathVariable final Integer id) {
        AcModule entity = entityService.getById(id);
        return new Result(true, entity);
    }

    @SysLog(action = "acModule:add", value = "角色管理：添加模块")
    @ApiOperation(value = "添加模块")
    @RequestMapping(method = {RequestMethod.POST})
    public Result add(@Valid @RequestBody final AcModule entity) {
        if (null == entity.getId()) {
            entity.setCreatetime(new Date());
            entity.setState("1");
        }
        entityService.save(entity);
        return new Result(true, "成功保存模块", null);
    }

    @SysLog(action = "acModule:update", value = "角色管理：修改模块")
    @ApiOperation(value = "修改模块")
    @RequestMapping(method = {RequestMethod.PUT})
    public Result update(@Valid @RequestBody final AcModule entity) {
        if (null == entity.getId()) {
            throw new RuntimeException("id不能为空");
        }
        entityService.updateById(entity);
        return new Result(true, "成功修改模块", null);
    }

    @SysLog(action = "acModule:all", value = "角色管理：全部模块")
    @ApiOperation(value = "全部模块")
    @RequestMapping(value = "all", method = RequestMethod.GET)
    public Result<List<AcModuleVo>> all(AcModuleVo example) {
        List<AcModuleVo> entitys = entityService.findByExample(example);
        if (null != entitys) {
            return new Result<>(true, entitys);
        }
        return new Result<>(true, Collections.emptyList());
    }

}

